package blog.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import blog.entity.Article;
import blog.entity.Comment;
import blog.entity.User;
import blog.service.ArticleService;
import blog.service.CommentService;
import blog.service.UserService;
/**
 * 用户控制层
 * @author 江佳洋
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private ArticleService articleService;
	@Autowired
	private CommentService commentService;
	/**
	 * 转向登陆界面
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String goLogin() {
		return "login";
	}
	
	/**
	 * 用户登录
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String test(HttpServletRequest request,ModelMap map) {
		String str = request.getParameter("userName");
		User user = userService.getUserByNameOrEmail(str);
		if(user!=null) {
			String password = request.getParameter("password");
			if(user.getUserPass().equals(password)) {
				List<Article> listRecentArticle = articleService.listRecentArticle(5);
				map.put("article", listRecentArticle);
				
				List<Comment> listRecentComment = commentService.listRecentComment(5);
				map.put("comment", listRecentComment);
				request.getSession().setAttribute("user", user);
				return "index";
			}else {
				request.setAttribute("msg", "密码错误");
				return "login";
			}
		}else {
			return "login";
		}
	}
	/**
	 * 去首页
	 * @param map
	 * @return
	 */
	@RequestMapping("/index")
	public String goIndex(ModelMap map) {
		List<Article> listRecentArticle = articleService.listRecentArticle(5);
		map.put("article", listRecentArticle);
		
		List<Comment> listRecentComment = commentService.listRecentComment(5);
		map.put("comment", listRecentComment);
		return "index";
	}
	/**
	 * 去添加用户
	 * @return
	 */
	@RequestMapping(value="/addUser",method = RequestMethod.GET)
	public String goAddUser() {
		return "user/user_add";
	}
	/**
	 * 添加用户
	 * @param map
	 * @param user
	 * @param photo 用户上传的照片
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/addUser",method = RequestMethod.POST)
	public String addUser(ModelMap map,User user,MultipartFile photo) throws IOException {
		user.setUserRegisterTime(new Date());
		user.setUserPhoto(photo.getBytes());
		user.setUserStatus(1);
		userService.addUser(user);
		map.put("user", user);
		map.put("msg", "添加成功");
		return "forward:/user";
	}
	/**
	 * 查询所有用户
	 * @param map
	 * @return
	 */
	@RequestMapping("")
	public String userList(ModelMap map) {
		List<User> list = userService.getUserList();
		map.put("userList", list);
		return "user/user_list";
	}
	/**
	 * 查询照片,回到客户端显示照片
	 * @param map
	 * @param userId
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/photo/{userId}")
	public void getPhoto(ModelMap map,@PathVariable("userId")Integer userId,HttpServletResponse response) throws IOException {
		User user = userService.getUserById(userId);
		response.setContentType("image/jsp");
		ServletOutputStream outputStream = response.getOutputStream();
		outputStream.write(user.getUserPhoto());
		outputStream.flush();
	}
	/**
	 * 去用户修改界面
	 * @param userId
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/editUser/{userId}",method=RequestMethod.GET)
	public String gousetEdit(@PathVariable("userId")Integer userId,ModelMap map) {
		User userById = userService.getUserById(userId);
		map.put("user", userById);
		return "user/user_edit";
	}
	/**
	 * 用户修改
	 * @param userId
	 * @param map
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/editUser",method=RequestMethod.POST)
	public String usetEdit(ModelMap map,User user,MultipartFile photo) throws IOException {
		if(photo.getOriginalFilename()!="") {//如果没有修改照片
			user.setUserPhoto(photo.getBytes());
		}
		userService.editUser(user);
		map.put("user", user);
		map.put("msg", "修改成功");
		return "user/user_edit";
	}
	/**
	 * 验证用户名是否重复
	 * @param userName
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/checkUserName")
	public void checkUserName(String userName,HttpServletResponse response) throws IOException {
		User user = userService.getUserByNameOrEmail(userName);
		if(user!=null) {
			response.getWriter().print("1");
		}
	}
	/**
	 * 验证邮箱是否重复
	 * @param userEmail
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/checkUserEmail")
	public void checkUserEmail(String userEmail,HttpServletResponse response) throws IOException {
		User user = userService.getUserByNameOrEmail(userEmail);
		if(user!=null) {
			response.getWriter().print("1");
		}
	}
	/**
	 * 删除用户
	 * @param userId
	 * @return
	 */
	@RequestMapping("deleteUser/{userId}")
	public String deleteUser(@PathVariable("userId")Integer userId) {
		userService.deleteUser(userId);
		return "forward:/user";
	}
	/**
	 * 注销
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.removeAttribute("user");
		session.invalidate();
		return "redirect:/jsp/login.jsp";
	}
	@RequestMapping("/profile")
	public String userProfile(HttpSession session,ModelMap map) {
		User attribute = (User)session.getAttribute("user");
		User user = userService.getUserById(attribute.getUserId());
		map.put("user", user);
		return "user/user_profile";
	}
	
}
